/**
 * 
 */
package cn.haona.game.simb.wap.servlet.filter;

import java.io.IOException;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;

import test.TestDB;

import cn.haona.game.simb.object.god.MainManager;
import cn.haona.game.simb.object.god.cache.Cache;
import cn.haona.game.simb.servlet.Define;
import cn.haona.game.simb.servlet.SessionHn;
import cn.haona.game.simb.servlet.SessionHnManager;
import cn.haona.game.simb.servlet.Util;
import cn.haona.game.simb.wap.servlet.WebDefine;

/**
 * @author Administrator
 *
 */
public class AddSessionFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest hr=(HttpServletRequest)request;
		
		//从RemoteCache中去得SessionHn对象
		Cache remoteCache=
			MainManager.getInstance().getCacheManager().getRemoteCache();
		
		System.out.println("通过了过滤器:"+hr.getRequestURI());
		SessionHn sessionHn=null;
		
		//查找请求中的SessionID
		String requestSessionID = request.getParameter(Define.SESSION_ID);
		//如果没有Session
		if(requestSessionID==null)
		{
			//创建一个Session
			sessionHn=createSessionAndputInCache(request);
		}
		//如果有Session
		else
		{
			//从缓存中得到Session对象
			sessionHn=(SessionHn)remoteCache.get(requestSessionID);
			//如果没取到Session对象
			if(sessionHn==null)
			{
				//创建一个Session
				sessionHn=createSessionAndputInCache(request);
			}
			else
			{
				//放到request当中
				request.setAttribute(Define.SESSION_ID, sessionHn);
			}
			
		}
		
//		//测试DB
//		TestDB testDB=new TestDB();
//		testDB.testDB();


		//继续穿过下面的过滤器
		chain.doFilter(request, response);
		

		//新的访问延续玩家会话
		String operateKey=(String)sessionHn.getElement(SessionHn.KEY_OPERATEID);
		System.out.println("operateKey="+operateKey);
//		SessionHnManager.saveSessionHn(sessionHn);
		Date dateExpired=new Date(System.currentTimeMillis()+WebDefine.SESSION_EXPIRED_TIME);
		remoteCache.set(requestSessionID, sessionHn
				,dateExpired);	
	}

	
	/**
	 * 创造一个Session对象并放入缓存中
	 * @param request
	 */
	private SessionHn createSessionAndputInCache(ServletRequest request)
	{
		//创建一个Session
		SessionHn sessionHn=new SessionHn();
		//创造一个sessionId
		String sessionId=Util.createSessionID();
		//放入Sessionid
		sessionHn.setId(sessionId);
		//放到request当中
		request.setAttribute(Define.SESSION_ID, sessionHn);
		//放到RemoteCache中
		Cache remoteCache=
		MainManager.getInstance().getCacheManager().getRemoteCache();
		Date dateExpired=new Date(System.currentTimeMillis()+WebDefine.SESSION_EXPIRED_TIME);
		boolean is=
		remoteCache.set(sessionHn.getId(), sessionHn
				,dateExpired);
//				);
//		System.out.println("remoteCache="+remoteCache);
		return sessionHn;
	}
	
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("初始化:"+this.toString());
		
	}

	
	public String toString() {
		return "过滤器AddSessionFilter";
	}

}
